package com.lovesoft.prologer.core;

import java.io.Serializable;

import com.lovesoft.prologer.core.storage.StorageMe;

/**
 * Identify log 
 * @author Patryk 2014.09.06
 */

public class LogID implements Serializable, StorageMe {
	private static final String PREFIX_FILE_PATH = "filePath=";
	private static final long serialVersionUID = -1155020709144131249L;
	private String filePath;
	// Here is the place for other id's like http address in future.

	public LogID() {
	}
	
	public LogID(String filePath) {
		this.filePath = filePath;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((filePath == null) ? 0 : filePath.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LogID other = (LogID) obj;
		if (filePath == null) {
			if (other.filePath != null)
				return false;
		} else if (!filePath.equals(other.filePath))
			return false;
		return true;
	}

	@Override
	public String storageToString() {
		return PREFIX_FILE_PATH + filePath;
	}

	@Override
	public void buildFromString(String data) {
		if(!data.startsWith(PREFIX_FILE_PATH)) {
			throw new RuntimeException("Data for LogID should start with "+ PREFIX_FILE_PATH + " but starting with: " + data);
		}
		String newFilePath = data.substring(PREFIX_FILE_PATH.length());
		if(newFilePath == null || newFilePath.isEmpty()) {
			throw new RuntimeException("FilePath is empty");
		}
		filePath = newFilePath;
	}

}
